package com.atguigu.redis7.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @版本： V5.1.0
 * @版权： Copyright @ 2025 广东广昱信息科技有限公司版权所有
 * @作者： 广东广昱信息科技有限公司
 * @日期： 2025-07-13 12:40
 */
@Service
@Slf4j
public class HyperLogService {
    @Resource
    private RedisTemplate redisTemplate;

    @PostConstruct
    public void initIP(){
        new Thread(() -> {
            String ip = null;
            for (int i = 0; i < 100; i++){
                Random random = new Random();
                ip = random.nextInt(256) + "." +
                        random.nextInt(256) + "." +
                        random.nextInt(256) + "." +
                        random.nextInt(256);
                long hll = redisTemplate.opsForHyperLogLog().add("hll",ip);
                log.info("****ip:{},该IP地址访问首页的次数={}",ip,hll);
                try {
                   TimeUnit.SECONDS.sleep(3);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            }

        },"t1").start();

    }

    public long uv(){

        return redisTemplate.opsForHyperLogLog().size("hll");
    }
}
